*&---------------------------------------------------------------------*
*& Report  YECC_BC_TAB_01
*& Загрузка и выгрузка данных - по Z-таблицам
*&---------------------------------------------------------------------*
*& Иван / 2013 / v0.95
*& http://code.google.com/p/abap-entropii-net/
*& Licensed under the Apache License, Version 2.0
*&---------------------------------------------------------------------*
*& [DONE] Добавить чёткую обработку ошибок при импорте данных в таблицу
*& [TODO] Разделить процедурно выгрузку и загрузку
*& [DONE] Добавить дату в формат выгрузки
*& [DONE] Добавить маску при выборе таблицы
*& [DONE] Разрулить кодировки

report  yecc_bc_tab_01.
type-pools: truxs, abap.
tables tadir.

select-options: so_tab for tadir-obj_name obligatory memory id dtb no intervals.
parameters: p_file type string obligatory default 'c:\TEMP\*.TXT'.

parameters: p_where type string.
selection-screen skip 1.
selection-screen comment /1(50) text-101.
parameters: rg_op1 radiobutton group opx default 'X' user-command dummy. "SAP -> File
parameters: rg_op2 radiobutton group opx.                                "File -> SAP
parameters: p_del  as checkbox.
selection-screen skip 1.
selection-screen comment /1(50) text-100.
parameters: rg_co1 radiobutton group cox default 'X'. "ANIS
parameters: rg_co2 radiobutton group cox.             "UTF-8

data: gt_tadir like tadir occurs 0 with header line.

at selection-screen output.
  loop at screen.
    if screen-name = 'P_DEL'.
      if rg_op2 = ' '.
        screen-input = '0'.
      else.
        screen-input = '1'.
      endif.
    endif.
    modify screen.
  endloop.

start-of-selection.
  select * from tadir into table gt_tadir
    where obj_name in so_tab  and pgmid = 'R3TR' and object = 'TABL'.

  loop at gt_tadir.
    perform process_table using gt_tadir-obj_name.
  endloop.

  zcl_zz_bal=>display( ).

end-of-selection.

*&---------------------------------------------------------------------*
*&      Form  process_table
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->LV_TAB     text
*----------------------------------------------------------------------*
form process_table using lv_tab. " type tabname16.
  data lv_dummy type string.
  data lr_exception type ref to cx_root.
  data: lv_error type string.

  field-symbols: <f_table>     type standard table.
  field-symbols: <f_structure> type any.

  try.

      data dref_ type ref to data.
      create data dref_ type standard table of (lv_tab).
      assign dref_->* to <f_table>.

      data dref__ type ref to data.
      create data dref__ type (lv_tab).
      assign dref__->* to <f_structure>.
    catch cx_root into lr_exception.
      zcl_zz_bal=>add_exc( lr_exception ).
      exit.
  endtry.

  data: lv_file type string.
  data: lv_date1 type string.
  data: lv_date2 type string.
  lv_file = p_file.
  concatenate sy-datum+6(2) sy-datum+4(2) sy-datum(4) into lv_date1 separated by '.'.
  lv_date2 = sy-datum.
  replace all occurrences of '*' in lv_file with lv_tab.
  replace all occurrences of '$DD' in lv_file with lv_date1. "DD.MM.YYYY
  replace all occurrences of '$D' in lv_file with lv_date2. "YYYYMMDD

  data: gd_codepage type abap_encoding.
  case 'X'.
    when rg_co1. gd_codepage = '1504'.
    when rg_co2. gd_codepage = '4310'.
  endcase.

  case 'X'.
    when rg_op1. "SAP->FILE
*      check p_tab(1) = 'Z'.

      try.
          select * from (lv_tab) into table <f_table> where (p_where).
        catch cx_root into  lr_exception. "CX_SY_DYNAMIC_OSQL_SEMANTICS
          zcl_zz_bal=>add_exc( lr_exception ).
          exit.
      endtry.

      call function 'GUI_DOWNLOAD'
        exporting
          filename              = lv_file
          filetype              = 'ASC'
          write_field_separator = 'X'
          codepage              = gd_codepage
        tables
          data_tab              = <f_table>
        exceptions
          others                = 99.

      if sy-subrc = 0.
        message i000(zzz_bal) into lv_dummy with 'Таблица выгружена в файл:' lv_tab '->' lv_file.
        zcl_zz_bal=>add_sy( ).
      else.
        "SY
        zcl_zz_bal=>add_sy( ).
        exit.
      endif.

    when rg_op2. "FILE-SAP
      check lv_tab(1) = 'Z'.

      call function 'GUI_UPLOAD'
        exporting
          filetype            = 'ASC'
          has_field_separator = 'X'
          filename            = lv_file
          codepage            = gd_codepage
        tables
          data_tab            = <f_table>
        exceptions
          others              = 99.
      if sy-subrc ne 0.
        zcl_zz_bal=>add_sy( ).
        exit.
      endif.

      try.
          if p_del = 'X'.
            delete from (lv_tab) where (p_where).
          endif.
        catch cx_root into lr_exception.
          zcl_zz_bal=>add_exc( lr_exception ).
      endtry.
      try.
          loop at <f_table> assigning <f_structure>.
            insert into (lv_tab) values <f_structure>.
            if sy-subrc = 4.
              message w000(zzz_bal) into lv_dummy with 'Такой ключ уже существует:' lv_tab '/' sy-tabix.
              zcl_zz_bal=>add_sy( ).
            endif.
          endloop.
        catch cx_root into lr_exception.
          zcl_zz_bal=>add_exc( lr_exception ).
      endtry.
      message i000(zzz_bal) into lv_dummy with 'Файл загружен в таблицу:' lv_file '->' lv_tab.
      zcl_zz_bal=>add_sy( ).
  endcase.

endform.                    "process_table